package com.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.domain.entity.Doctor;
import com.common.domain.entity.Goods;
import com.common.domain.entity.Leave;
import com.common.domain.entity.ResponseResult;
import com.common.domain.vo.LeaveInfoVo;
import com.common.mapper.DoctorMapper;
import com.common.mapper.GoodsMapper;
import com.common.mapper.LeaveMapper;
import com.common.service.GoodsService;
import com.common.service.LeaveService;
import com.common.util.SecurityUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.awt.*;
import java.util.List;

/**
* @author pc
* @description 针对表【sys_role_menu(角色和菜单关联表)】的数据库操作Service
* @createDate 2023-02-07 12:19:15
*/
@Service
public class LeaveImpl extends ServiceImpl<LeaveMapper, Leave>
        implements LeaveService {

    @Autowired
    private LeaveMapper leaveMapper;
    @Autowired
    private DoctorMapper doctorMapper;

    @Override
    public ResponseResult allLeaveList(Integer pageNum, Integer pageSize, Long userId, String startTime,String endTIme, String type) {
        PageHelper.startPage(pageNum,pageSize);

        List<LeaveInfoVo> list =  leaveMapper.allLeaveList(userId, startTime, endTIme, type);

        PageInfo<LeaveInfoVo> pageInfo = new PageInfo<>(list);

        pageInfo.setList(list);

        return ResponseResult.okResult(pageInfo);
    }

    @Override
    public ResponseResult updateLeaveStatus(Long id, String reason, String type) {
        LambdaQueryWrapper<Leave> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Leave::getId,id);
        Leave leave = new Leave();
        leave.setUpdateBy(SecurityUtils.getUserId());
        if(StringUtils.hasText(reason)){
            leave.setRejectReason(reason);
        }
        leave.setStatus(type);

        update(leave, queryWrapper);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getLeaveInfoList(Integer pageNum, Integer pageSize,String status) {
        PageHelper.startPage(pageNum,pageSize);

        List<LeaveInfoVo> list =  leaveMapper.allLeaveList(SecurityUtils.getUserId(), null, null, status);

        PageInfo<LeaveInfoVo> pageInfo = new PageInfo<>(list);

        pageInfo.setList(list);

        return ResponseResult.okResult(pageInfo);
    }
}
